﻿using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Ensemble_Windows.UI.Core
{
    /// <summary>
    ///     界面皮肤
    /// </summary>
    public sealed class FormSkin : ContainerControl
    {
        private Color _baseColor = Helpers.BorderColor;
        private Color _borderColor = Helpers.BorderColor;
        private bool _cap;
        private Color _flatColor = Helpers.BaseColor;
        private int _h;

        private Color _headerColor = Helpers.HeaderColor;
        private Point _mousePoint = new Point(0, 0);
        private int _w;
        private readonly int MoveHeight = 50;
        private readonly Color _textColor = Helpers.TextColor;

        /// <summary>
        ///     TextLight
        /// </summary>
        public Color TextLight = Helpers.TextLight;

        /// <summary>
        ///     窗体皮肤
        /// </summary>
        public FormSkin()
        {
            MouseDoubleClick += FormSkin_MouseDoubleClick;
            SetStyle(
                ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            BackColor = Color.White;
            Font = new Font("微软雅黑", 12);
        }

        /// <summary>
        ///     HeaderColor
        /// </summary>
        [Category("Colors")]
        public Color HeaderColor
        {
            get { return _headerColor; }
            set { _headerColor = value; }
        }

        /// <summary>
        ///     BaseColor
        /// </summary>
        [Category("Colors")]
        public Color BaseColor
        {
            get { return _baseColor; }
            set { _baseColor = value; }
        }

        /// <summary>
        ///     BorderColor
        /// </summary>
        [Category("Colors")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }

        /// <summary>
        ///     FlatColor
        /// </summary>
        [Category("Colors")]
        public Color FlatColor
        {
            // get { return Helpers.FlatColor; }
            // set { Helpers.FlatColor = value; }
            get { return _flatColor; }
            set { _flatColor = value; }
        }

        /// <summary>
        ///     HeaderMaximize
        /// </summary>
        [Category("Options")]
        public bool HeaderMaximize { get; set; }

        /// <summary>
        ///     按钮按下
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if ((e.Button == MouseButtons.Left) & new Rectangle(0, 0, Width, MoveHeight).Contains(e.Location))
            {
                _cap = true;
                _mousePoint = e.Location;
            }
        }

        /// <summary>
        ///     鼠标双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormSkin_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (HeaderMaximize)
                if ((e.Button == MouseButtons.Left) & new Rectangle(0, 0, Width, MoveHeight).Contains(e.Location))
                {
                    var findForm = FindForm();
                    if (findForm != null && findForm.WindowState == FormWindowState.Normal)
                    {
                        var form = FindForm();
                        if (form != null) form.WindowState = FormWindowState.Maximized;
                        var o = FindForm();
                        if (o != null) o.Refresh();
                    }
                    else
                    {
                        var form = FindForm();
                        if (form != null && form.WindowState == FormWindowState.Maximized)
                        {
                            var o = FindForm();
                            if (o != null) o.WindowState = FormWindowState.Normal;
                            var findForm1 = FindForm();
                            if (findForm1 != null) findForm1.Refresh();
                        }
                    }
                }
        }

        /// <summary>
        ///     鼠标弹起
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _cap = false;
        }

        /// <summary>
        ///     鼠标移动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (_cap)
                Parent.Location = new Point(
                    MousePosition.X - _mousePoint.X,
                    MousePosition.Y - _mousePoint.Y
                );
        }

        /// <summary>
        ///     创建控件
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (ParentForm != null)
            {
                ParentForm.FormBorderStyle = FormBorderStyle.None;
                ParentForm.AllowTransparency = false;
                ParentForm.TransparencyKey = Color.Fuchsia;
                var findForm = ParentForm.FindForm();
                if (findForm != null) findForm.StartPosition = FormStartPosition.CenterScreen;
            }
            Dock = DockStyle.Fill;
            Invalidate();
        }

        /// <summary>
        ///     重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            var b = new System.Drawing.Bitmap(Width, Height);
            var g = Graphics.FromImage(b);
            _w = Width;
            _h = Height;

            var Base = new Rectangle(0, 0, _w, _h);
            var header = new Rectangle(0, 0, _w, 50);

            var with2 = g;
            with2.SmoothingMode = SmoothingMode.HighQuality;
            with2.PixelOffsetMode = PixelOffsetMode.HighQuality;
            with2.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            with2.Clear(BackColor);

            //-- Base
            with2.FillRectangle(new SolidBrush(_baseColor), Base);

            //-- Header
            with2.FillRectangle(new SolidBrush(_headerColor), header);

            //-- Logo
            with2.FillRectangle(new SolidBrush(Color.FromArgb(243, 243, 243)), new Rectangle(8, 16, 4, 18));
            with2.FillRectangle(new SolidBrush(FlatColor), 16, 16, 4, 18);
            with2.DrawString(Text, Font, new SolidBrush(_textColor), new Rectangle(26, 15, _w, _h), Helpers.NearSf);

            //-- Border
            with2.DrawRectangle(new Pen(_borderColor), Base);

            base.OnPaint(e);
            g.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(b, 0, 0);
            b.Dispose();
        }
    }
}